package com.njlime.cms.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.njlime.cms.entities.WxMenu;
import com.njlime.cms.service.sys.WxMenuService;
import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.sdk.wechat.WeChatUtil;
import com.njlime.core.sdk.wechat.jsapi.AccessToken;
import com.njlime.core.utils.StringUtil;
import com.njlime.core.web.validation.annotation.support.ValidateService;
import com.njlime.sys.entities.Unit;
import com.njlime.sys.entities.UnitWxConfig;
import com.njlime.sys.security.util.UserUtils;
import com.njlime.sys.service.UnitService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 标签控制器
 * Created by Dean on 2017/8/10.
 */
@Controller
@RequestMapping(value = "/cms/wxMenu")
public class WxMenuController extends BaseController {

    @Resource
    private UnitService unitService;
    @Resource
    private WxMenuService wxMenuService;

    //--------------------控制器-------------------------

    /**
     * 跳转标签管理页面
     * @return
     */
    @RequestMapping(value = "menuManage")
    public ModelAndView infoManage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/content/channel/wxMenuManage");
        return modelAndView;
    }


    //------------------功能--------------------------

    /**
     * 获取菜单
     * @param request
     * @return
     */
    @RequestMapping(value = "menuList", method = RequestMethod.GET)
    @ResponseBody
    public String menuList(HttpServletRequest request) {
        Unit rootUnit = UserUtils.getUser().getRootUnit();
        if(rootUnit != null){
            UnitWxConfig unitWxConfig = unitService.getWxConfig(rootUnit.getId());
            if(unitWxConfig != null && !StringUtil.isEmpty(unitWxConfig.getApp_id())){
                return toJsonListData(wxMenuService.wxMenuList(rootUnit.getId()));
            }
        }
        return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION,"请先绑定微信公众账号");
    }

    /**
     * 获取菜单
     * @param request
     * @return
     */
    @RequestMapping(value = "getMenu", method = RequestMethod.GET)
    @ResponseBody
    public String getMenu(HttpServletRequest request, Integer id) {
        Unit rootUnit = UserUtils.getUser().getRootUnit();
        if(rootUnit != null){
            UnitWxConfig unitWxConfig = unitService.getWxConfig(rootUnit.getId());
            if(unitWxConfig != null && !StringUtil.isEmpty(unitWxConfig.getApp_id())){
                return toJsonSuccessData(wxMenuService.findOne(id));
            }
        }else{
            return toJsonErrorData(ExceptionState.MANAGER_EXCEPTION);
        }
        return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION,"请先绑定微信公众账号");
    }

    /**
     * 保存
     * @param request
     * @param wxMenu
     * @return
     */
    @RequestMapping(value = "saveWxMenu", method = RequestMethod.POST)
    @ResponseBody
    public String saveWxMenu(HttpServletRequest request, WxMenu wxMenu) {
        Unit rootUnit = UserUtils.getUser().getRootUnit();
        if(rootUnit != null){
            wxMenu.setUnit_id(rootUnit.getId());
            if("click".equals(wxMenu.getType())){
                String[] key = wxMenu.getEkey().split("\\|");
                if(key.length == 2){
                    wxMenu.setEkey(key[0] + "|" + rootUnit.getId() + "|" + key[1]);
                }
            }
        } else {
            return toJsonErrorData(ExceptionState.MANAGER_EXCEPTION);
        }
        return toJsonSuccessData(wxMenuService.saveWxMenu(wxMenu));
    }

    /**
     * 删除
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "delWxMenu", method = RequestMethod.POST)
    @ResponseBody
    public String delWxMenu(HttpServletRequest request, Integer id) {
        wxMenuService.delWxMenu(id);
        return toJsonSuccessData();
    }

    /**
     * 发布
     * @param request
     * @return
     */
    @RequestMapping(value = "publish", method = RequestMethod.POST)
    @ResponseBody
    public String publish(HttpServletRequest request) {
        Unit rootUnit = UserUtils.getUser().getRootUnit();
        if(rootUnit != null){
            UnitWxConfig unitWxConfig = unitService.getWxConfig(rootUnit.getId());
            if(unitWxConfig != null && !StringUtil.isEmpty(unitWxConfig.getApp_id())) {
                int code = wxMenuService.publish(rootUnit.getId(),unitWxConfig);
                if(code == 0){
                    return toJsonSuccessData();
                }else{
                    return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION,"提交失败,请联系管理员");
                }
            }

        }
        return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION,"请先绑定微信公众账号");
    }

}
